package cn.sms.org.controller;

import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.DictionaryType;
import cn.sms.org.query.DictionaryTypeQuery;
import cn.sms.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService iDictionaryTypeService;

    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id){
        return iDictionaryTypeService.queryById(id);
    }

    @GetMapping
    public List<DictionaryType> queryAll(){
        return iDictionaryTypeService.queryAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iDictionaryTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PutMapping
    public AjaxResult save(@RequestBody DictionaryType dictionaryType){
        System.out.println(dictionaryType);
        try {
            if (dictionaryType.getId()==null){
                iDictionaryTypeService.add(dictionaryType);
            }else {
                iDictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/list")
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query){
        return iDictionaryTypeService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult batchRemoveDictionaryType(@RequestBody List<Long> ids){
        try {
            iDictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败哟");
        }
    }

}
